<?php


namespace App\Controller\Frontend;


use App\Entity\Feedback;
use App\Entity\Page;
use App\Entity\PageComment;
use App\Service\Redis;
use App\Utils\Traits\JsonTrait;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class AboutMeController extends AbstractController {
    use JsonTrait;

    private $redis;

    public function __construct (Redis $client) {
        $this->redis = $client->redis;
    }

    /**
     * @param Request $request
     * @Route("/about_me", name="about_me_page")
     * @Template()
     * @return array
     */
    public function index (Request $request) {
        $page_total = $this->getDoctrine()->getRepository(Page::class)->getPageTotal(true);
        $page_comment_total = $this->getDoctrine()->getRepository(PageComment::class)->getCommentTotal(true);
        $feedback_total = $this->getDoctrine()->getRepository(Feedback::class)->getFeedbackTotal();
        $page_view_total = $this->getDoctrine()->getRepository(Page::class)->getPageViewTotal(true);
        return [
            'page_total'         => $page_total,
            'page_comment_total' => $page_comment_total,
            'feedback_total'     => $feedback_total,
            'page_view_total'    => $page_view_total,
        ];
    }

    /**
     * 留言
     * @param Request $request
     * @Route("/about_me/feedback", name="about_me_feedback", methods={"POST"})
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     * @throws \Exception
     */
    public function feedback (Request $request) {
        if ($request->isXmlHttpRequest()) {
            // 避免频繁发布留言，同一个IP，一小时只允许留言5次
            $ip = $request->getClientIp();
            $redis_key = $this->getParameter('app.name').':about_me:feedback:'.$ip;
            if ($this->redis->exists($redis_key)) {
                $redis_value = $this->redis->get($redis_key);
                $redis_ttl = $this->redis->ttl($redis_key);
                if ($redis_value >= 5) {
                    return $this->jsonFailure('留言过于频繁，请稍微再操作');
                }
                $this->redis->incr($redis_key);
                $this->redis->expire($redis_key, $redis_ttl);
            } else {
                $this->redis->setex($redis_key, 3600, 1);
            }
            // 添加留言
            $name = $request->request->get('name', '未知用户');
            $email = $request->request->get('email', null);
            $message = $request->request->get('message', null);
            $feedback = new Feedback();
            $feedback->setNickname($name);
            $feedback->setEmail($email);
            $feedback->setContent($message);
            $feedback->setIp($request->getClientIp());
            $feedback->setCreatedAt(new \DateTime());
            $this->getDoctrine()->getManager()->persist($feedback);
            $this->getDoctrine()->getManager()->flush();
            return $this->jsonSuccess();
        } else {
            return $this->jsonFailure('请求错误');
        }
    }

}